﻿using LDM.Framework.ExceptionHandling.Extensions;
using LDM.Sys.Repository.Interfaces;
using LDM.Sys.Service.Controllers.Base;
using LDM.Sys.ServiceAPI.Model.DBModel;
using LDM.Sys.ServiceAPI.Model.ViewModel.Request.AuthorityReq;
using LDM.Sys.ServiceAPI.Model.ViewModel.Response.Authority;
using Microsoft.AspNetCore.Mvc;
using Model.Models;
using Utility.Extensions;

namespace LDM.Sys.Service.Controllers.Authority
{
    /// <summary>
    /// 后台管理用户信息服务
    /// </summary>
    public class SysAccountController : BaseController
    {
        private readonly ISysAccountRepository _sysAccountRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        public SysAccountController(ISysAccountRepository sysAccountRepository)
        {
            _sysAccountRepository = sysAccountRepository;
        }

        #region 基础查询
        /// <summary>
        /// 获取后台管理用户信息列表
        /// </summary>
        [HttpPost]
        public PageResult<SysAccount> GetSysAccounts(GetSysAccountReq request)
        {
            request.ThrowIfNull();
            return _sysAccountRepository.GetSysAccounts(request);
        }

        /// <summary>
        /// 添加后台管理用户信息
        /// </summary>
        [HttpPost]
        public void AddSysAccount(AddSysAccountReq request)
        {
            request.ThrowIfNull();
            var obj = request.Copy<AddSysAccountReq, SysAccount>();
            _sysAccountRepository.Insert(obj);
        }
        /// <summary>
        /// 修改后台管理用户信息
        /// </summary>
        [HttpPost]
        public void UpdateSysAccount(UpdateSysAccountReq request)
        {
            request.ThrowIfNull();
            var getKey = _sysAccountRepository.GetByKey(request.Id);
            getKey.ThrowIfNull();
            var obj = request.Copy<UpdateSysAccountReq, SysAccount>();
            _sysAccountRepository.Update(obj);
        }
        /// <summary>
        /// 删除后台管理用户信息
        /// </summary>
        [HttpPost]
        public void DelSysAccount(DelSysAccountReq request)
        {
            request.ThrowIfNull();
            var getKey = _sysAccountRepository.GetByKey(request.Id);
            getKey.ThrowIfNull();
            _sysAccountRepository.Delete(request.Id);
        }
        #endregion

        #region 业务查询
        /// <summary>
        /// 查询所有用户简要信息
        /// </summary>
        [HttpPost]
        public List<BriefAccountRes> BriefAccounts()
        {
            return _sysAccountRepository.BriefAccounts().ToList();
        }
       

        #endregion
    }
}
